package com.selfstudy.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.selfstudy.common.core.domain.model.LoginUser;
import com.selfstudy.common.utils.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.selfstudy.common.annotation.Log;
import com.selfstudy.common.core.controller.BaseController;
import com.selfstudy.common.core.domain.AjaxResult;
import com.selfstudy.common.enums.BusinessType;
import com.selfstudy.system.domain.SMsg;
import com.selfstudy.system.service.ISMsgService;
import com.selfstudy.common.utils.poi.ExcelUtil;
import com.selfstudy.common.core.page.TableDataInfo;

/**
 * 投诉留言Controller
 * 
 * @author lgf
 * @date 2024-01-30
 */

@RestController
@RequestMapping("/system/msg")
@Api("投诉反馈")
public class SMsgController extends BaseController
{
    @Autowired
    private ISMsgService sMsgService;

    /**
     * 查询投诉留言列表
     */
    @ApiOperation("投诉留言列表")
    @PreAuthorize("@ss.hasPermi('system:msg:list')")
    @GetMapping("/list")
    public TableDataInfo list(SMsg sMsg)
    {
        startPage();
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if(SecurityUtils.hasRole("teacher") || SecurityUtils.isAdmin(loginUser.getUserId())){
            //查询所有
            sMsg.setUsername(null);
        }else{
            sMsg.setUsername(loginUser.getUsername());
        }
        List<SMsg> list = sMsgService.selectSMsgList(sMsg);
        return getDataTable(list);
    }

    /**
     * 导出投诉留言列表
     */
    @PreAuthorize("@ss.hasPermi('system:msg:export')")
    @Log(title = "投诉留言", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SMsg sMsg)
    {
        List<SMsg> list = sMsgService.selectSMsgList(sMsg);
        ExcelUtil<SMsg> util = new ExcelUtil<SMsg>(SMsg.class);
        util.exportExcel(response, list, "投诉留言数据");
    }

    /**
     * 获取投诉留言详细信息
     */
    @ApiOperation("详情")
    @ApiImplicitParam(name = "id", value = "详情id", required = true, dataType = "int", paramType = "path", dataTypeClass = Integer.class)
    @PreAuthorize("@ss.hasPermi('system:msg:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(sMsgService.selectSMsgById(id));
    }

    /**
     * 新增投诉留言
     */
    @ApiOperation("新增")
    @PreAuthorize("@ss.hasPermi('system:msg:add')")
    @Log(title = "投诉留言", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SMsg sMsg)
    {
        return toAjax(sMsgService.insertSMsg(sMsg));
    }

    /**
     * 修改投诉留言
     */
    @ApiOperation("修改")
    @PreAuthorize("@ss.hasPermi('system:msg:edit')")
    @Log(title = "投诉留言", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SMsg sMsg)
    {
        return toAjax(sMsgService.updateSMsg(sMsg));
    }

    /**
     * 删除投诉留言
     */
    @ApiOperation("删除")
    @PreAuthorize("@ss.hasPermi('system:msg:remove')")
    @Log(title = "投诉留言", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(sMsgService.deleteSMsgByIds(ids));
    }
}
